﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for UserBusiness
/// </summary>
public class UserBusiness
{
    UserData user = null;
	public UserBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
        user = new UserData();
	}

    public bool add(UsersEntities item)
    {
        return user.add(item);
    }
    public bool editU_P(UsersEntities item)
    {
        return user.editU_P(item);
    }
    public bool edit(UsersEntities item)
    {
        return user.edit(item);
    }
    public bool delete(int id)
    {
        return user.delete(id);
    }
    public bool checkUser(string username_new,string username_old)
    {
        bool flat = true;
        System.Collections.ArrayList items = this.getUser();
        if (items != null)
        {
            UsersEntities item = null;
            if (username_old == null || username_old.Equals(""))//add
            {
                for (int i = 0; i < items.Count; i++)
                {
                    item = (UsersEntities)items[i];
                    if (username_new.Equals(item._userName))
                    {
                        flat = false;
                        break;
                    }
                }
            }
            else
            {
                if (!username_new.Equals(username_old))//edit username
                {
                    for (int i = 0; i < items.Count; i++)
                    {
                        item = (UsersEntities)items[i];
                        if (username_new.Equals(item._userName))
                        {
                            flat = false;
                            break;
                        }
                    }
                }
            }
        }
        return flat;
    }
    public System.Collections.ArrayList getUser()
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        DataTable dt = user.getUser(0);
        if (dt != null)
        {
            UsersEntities item = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                item = new UsersEntities();
                item._userID = int.Parse(row["id"].ToString().Trim());
                item._userName = row["username"].ToString();
                item._pass = row["password"].ToString();
                item._name = row["name"].ToString();
                item._email = row["email"].ToString();
                item._messenger = row["messenger"].ToString();
                item._mobile = row["mobile"].ToString();
                item._roleID = int.Parse(row["role_id"].ToString());
                item._position = row["position"].ToString();
                item._birthday = row["birthday"].ToString();
                item._address = row["address"].ToString();
                item._gender = row["gender"].ToString();
                item._display = row["display"].ToString();
                item._status = row["status"].ToString();
                items.Add(item);
                item = null;
            }
        }
        return items;
    }

    public UsersEntities getItem(int id)
    {
        UsersEntities item = null;
        if (id > 0)
        {
            DataTable dt = user.getUser(id);
            if (dt != null)
            {
                DataRow row = dt.Rows[0];
                item = new UsersEntities();
                item._userID = int.Parse(row["id"].ToString().Trim());
                item._userName = row["username"].ToString();
                item._pass = row["password"].ToString();
                item._name = row["name"].ToString();
                item._email = row["email"].ToString();
                item._messenger = row["messenger"].ToString();
                item._mobile = row["mobile"].ToString();
                item._roleID = int.Parse(row["role_id"].ToString());
                item._position = row["position"].ToString();
                item._birthday = row["birthday"].ToString();
                item._address = row["address"].ToString();
                item._gender = row["gender"].ToString();
                item._display = row["display"].ToString();
                item._status = row["status"].ToString();
            }
        }
        return item;
    }
    public UsersEntities login(string username, string password)
    {
        UsersEntities item = null;
        if (!username.Equals("") && !password.Equals(""))
        {
            System.Data.SqlClient.SqlDataReader row = user.login(username, password);
            if (row.HasRows)
            {
                if (row.Read())
                {
                    item = new UsersEntities();
                    item._userID = int.Parse(row["id"].ToString().Trim());
                    item._userName = row["username"].ToString();
                    item._pass = row["password"].ToString();
                    item._name = row["name"].ToString();
                    item._email = row["email"].ToString();
                    item._messenger = row["messenger"].ToString();
                    item._mobile = row["mobile"].ToString();
                    item._roleID = int.Parse(row["role_id"].ToString());
                    item._position = row["position"].ToString();
                    item._birthday = row["birthday"].ToString();
                    item._address = row["address"].ToString();
                    item._gender = row["gender"].ToString();
                    item._display = row["display"].ToString();
                    item._status = row["status"].ToString();
                }
                row.Close();
            }
        }
        return item;
    }

    public System.Collections.ArrayList GetUserView()
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        System.Data.SqlClient.SqlDataReader row = user.GetUserView();
        if (row.HasRows)
        {
            UsersEntities item = null;
           while(row.Read())
           {
                item = new UsersEntities();
                item._name = row["rolename"].ToString();
                item._messenger = row["messenger"].ToString();
                item._mobile = row["mobile"].ToString();
                items.Add(item);
                item = null;
            }
           row.Close();
        }
        return items;
    }
}
